<?php
class Blog{
	private $db;

	function __construct($db){
		$this->db = $db;
	}

	public function getArticles(){
		$query = $this->db->select("SELECT id, timestamp, title, header FROM posts WHERE 1='%s' ORDER BY id DESC",1);
		$result = $query->result;
		if ($result) {
			$articles = array();
			while($row = mysql_fetch_assoc($result)) {
				$post = new Article($row['id'], $row['timestamp'], $row['title'], $row['header']);
				if (isset($_GET['article']) && $_GET['article']== $row['id']) {
					$post->body =  $this->getArticleBodyAsHtml($row['id']);
				}
				$articles[] = $post;
			}
			return $articles;
		} else {
			return false;
		}
	}

	private function getArticleBodyAsXml($id){
		//TODO
	}
	private function getArticleBodyAsHtml($id){
		$query = $this->db->select("SELECT body FROM posts WHERE id='%s'",$id);
		$article = $query->getRow();
		$body = $article[0];
		return $body;
	}

	private function getArticleAsXml($id){
		//TODO for RSS
	}

	private function getArticleAsHtml($id){
		//TODO
	}

	public function getArticle($part, $id){
		if ($part == "body") {
			return $this->getArticleBodyAsHtml($id);
		}else{
			return $this->getArticleAsHtml($id);;
		}

	}
}



class Article{
	private $id, $timestamp, $title, $header, $body;

	function __construct($id, $timestamp, $title, $header){
		$this->id = $id;
		$this->timestamp = $timestamp;
		$this->title = $title;
		$this->header = $header;
	}


	function __get($key){
		return $this->$key;
	}

	function __set($key,$val) {
		$this->$key=$val;
	}


}

?>